<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Forms\BrandForm;
use App\Modules\Shop\Models\Category;
use App\Modules\System\Models\Brand;
use App\Modules\Shop\Shop;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BrandController extends Controller
{

    public function index(Request $request)
    {
        //更新分类中层级对应关系
//        $c = Brand::query()->get();
//        foreach ($c as $v) {
//            $cc = Category::query()->where("name", $v->parent)->first();
//            Brand::query()->where("id", $v->id)->update(["pid" => $cc->id, "parent" => $cc->name]);
//        }
//        dd("同步数据库结束");
        $this->checkAuth('merchant_staff_good-brand', 'good-brand');
        $shopId = $request->input('shop_id');
        $name = $request->input('name', '');
        $category = $request->input('category_id', '');
        $data = Brand::query()
            ->when(!empty($name), function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . "%");
            })
            ->when(!empty($category), function ($query) use ($category) {
                return $query->where('pid', $category);
            })
            ->where('shop_id', $shopId)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate(20);
        $shopData = Shop::query()->find($shopId);
        if ($shopData->category_ids) {
            $allCategory = Category::query()->whereIn("id", $shopData->category_ids)->pluck('name', 'id')->all();
        } else {
            $allCategory = [];
            if($shopData->category){
                $allCategory = Category::query()->whereIn("name", $shopData->category)->pluck('name', 'id')->all();
            }
        }
        $sidebar = 'brand';
        return view('brand.index', compact('data', 'shopId', 'sidebar', 'allCategory', 'category', 'name'));
    }

    public function create(Request $request)
    {
        $this->checkAuth('merchant_staff_brand-add', 'brand-add');

        $shopId = $request->input('shop_id');

        $shopData = Shop::query()->find($shopId);
        if ($shopData->category_ids) {
            $allCategory = Category::query()->whereIn("id", $shopData->category_ids)->pluck('name', 'id')->all();
        } else {
            $allCategory = [];
            if($shopData->category){
                $allCategory = Category::query()->whereIn("name", $shopData->category)->pluck('name', 'id')->all();
            }
        }

        $form = $this->form(BrandForm::class, [
            'url' => route('admin.brand.store', ['shop_id' => $shopId]),
            'method' => 'POST',
            'model' => [
                'parent' => $request->input('parent')
            ]
        ],[
            'all_category' => $allCategory
        ]);
       
        return view('brand.form', compact('form', 'shopId', 'allCategory'));
    }

    public function store(Request $request)
    {
        $this->checkAuth('merchant_staff_brand-add', 'brand-add');
        $shopId = $request->input('shop_id');
        $form = $this->form(BrandForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $categoryName = Category::query()->find($data['pid'])['name'];
        $data['parent'] = $categoryName;
        $data['shop_id'] = $shopId;
        Brand::query()->create($data);
        return redirect()->route('admin.brand.index', ['shop_id' => $data['shop_id']]);
    }

    public function edit(Brand $brand, Request $request)
    {
        $this->checkAuth('merchant_staff_brand-edit', 'brand-edit');
        $shopId = $request->input('shop_id');

        $shopData = Shop::query()->find($shopId);
        if ($shopData->category_ids) {
            $allCategory = Category::query()->whereIn("id", $shopData->category_ids)->pluck('name', 'id')->all();
        } else {
            $allCategory = [];
            if($shopData->category){
                $allCategory = Category::query()->whereIn("name", $shopData->category)->pluck('name', 'id')->all();
            }
        }

        $form = $this->form(BrandForm::class, [
            'url' => route('admin.brand.update', ['brand' => $brand, 'shop_id' => $shopId]),
            'method' => 'PUT',
            'model' => $brand
        ],[
            'all_category' => $allCategory
        ]);
        return view('brand.form', compact('form'));
    }

    public function update(Brand $brand, Request $request)
    {
        $this->checkAuth('merchant_staff_brand-edit', 'brand-edit');
        $shopId = $request->input('shop_id');
        $form = $this->form(BrandForm::class);
        if (!$form->isValid()) {
            return redirect()->back()->withErrors($form->getErrors())->withInput();
        }
        $data = $form->getFieldValues();
        $categoryName = Category::query()->find($data['pid'])['name'];
        $data['parent'] = $categoryName;
        $brand->update($data);
        return redirect()->route('admin.brand.index', ['shop_id' => $shopId]);
    }

    public function destroy(Brand $brand)
    {
        $this->checkAuth('merchant_staff_brand-delete', 'brand-delete');
        $brand->delete();
        return redirect()->route('admin.brand.index', ['shop_id' => $brand->shop_id]);
    }

    public function getSelectBrand(Request $request)
    {
        $parent = $request->input('parent');
        $re = Brand::query()
            ->where('pid', $parent)
            ->get()
            ->pluck('name', 'id');
        return $re;
    }

    public function changeSort(Request $request)
    {
        $id = $request->input('id');
        $sort = $request->input('sort');
        Brand::query()
            ->where('id', $id)
            ->update(['sort' => $sort]);
        return redirect()->back();
    }
}
